#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;


//实现前缀树，想啥得啥（哈哈哈哈）

class TreeNode1{
public:
    TreeNode1(){
        this->path = 0;
        this->end = 0;
    }
public:
    int path;
    int end;
    TreeNode1* next[26] = {nullptr};

};

class Trie {
public:
    /** Initialize your data structure here. */
    Trie() {
        
        
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        TreeNode1* curr = root;
        for(int i = 0;i<word.size();++i){
            int index = static_cast<int>(word[i]-'a');
            if(curr->next[index]==nullptr)
                curr->next[index] = new TreeNode1();
            ++curr->next[index]->path;
            curr = curr->next[index];
        }
        ++curr->end;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        TreeNode1* curr = root;
        for(int i = 0;i<word.size();++i){
            int index = static_cast<int>(word[i]-'a');
            if(curr->next[index]==nullptr){
                return false;
            }
            curr = curr->next[index];
        }
        if(curr->end>0){
            return true;
        }
        return false;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        //基本和search一样
        TreeNode1* curr = root;
        for(int i = 0;i<prefix.size();++i){
            int index = static_cast<int>(prefix[i]-'a');
            if(curr->next[index]==nullptr){
                return false;
            }
            curr = curr->next[index];
        }
        return true;
    }
public:
    TreeNode1* root = new TreeNode1();
};

/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */

int main(){
    Trie* obj = new Trie();
    obj->insert("app");
    obj->insert("apple");
    cout<<obj->search("apps")<<endl;
    cout<<obj->search("app")<<endl;
   
}